package com.interesting.client.UI.video;

import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.interesting.R;
import com.interesting.base.core.BaseActivity;
import com.interesting.client.support.video.VideoPlayer;

public class VideoActivity extends BaseActivity implements OnClickListener, OnSeekBarChangeListener {

	private SurfaceView surfaceView;
	private ImageView restartBtn;
	private CheckBox start;
	private SeekBar skbProgress;
	private VideoPlayer player;
	int progress;
	Uri uri;

	// = Uri.parse("http://192.168.1.14:8088/drunkenFun/video/test.mp4");

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		getSupportActionBar().hide();
		setContentView(R.layout.video_video_activity);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

		// uri = Uri.parse(getIntent().getExtras().getString("url"));
		uri = Uri.parse("http://s1.dwstatic.com/group1/M00/C8/B5/7c9bdbb32b9bdbe218768d157b76f2c0.gif");
		surfaceView = (SurfaceView) findViewById(R.id.video_activity_surfaceView);
		start = (CheckBox) findViewById(R.id.video_activity_start);
		restartBtn = (ImageView) findViewById(R.id.video_activity_restart);
		skbProgress = (SeekBar) findViewById(R.id.video_activity_skbProgress);

		player = new VideoPlayer(surfaceView, skbProgress);

		skbProgress.setOnSeekBarChangeListener(this);
		start.setOnClickListener(this);
		restartBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.video_activity_start:
			if (player.isPaused()) {
				player.start();
			} else if (player.isPlaying()) {
				player.pause();
			} else {
				player.playUrl(VideoActivity.this, uri);
			}
			break;
		case R.id.video_activity_restart:
			player.playUrl(VideoActivity.this, uri);
			break;
		}
	}
	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		this.progress = progress * player.mediaPlayer.getDuration() / seekBar.getMax();
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		player.mediaPlayer.seekTo(progress);
	}

}
